package tcp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class TcpServer {

	public static byte[] bs=new byte[1000];
	public static RandomAccessFile raf;
	public static OutputStream os;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//open port
		ServerSocket ss=new ServerSocket(8887);
		Socket acceptS=ss.accept();  //open port action
		System.out.println("server connection............");
		os=acceptS.getOutputStream();
		raf=new RandomAccessFile( "test.txt","rw");
		byte[] bs=new byte[100];
		long lengthAll=raf.length();
		System.out.println(lengthAll);
		List<Integer> length=new ArrayList<Integer>();
		length.add(0);
		
		ExecutorService pool= Executors.newFixedThreadPool(5);
		ServerPoint sp1=new ServerPoint("sp1",length);
		ServerPoint sp2=new ServerPoint("sp2",length);
		ServerPoint sp3=new ServerPoint("sp3",length);
		ServerPoint sp4=new ServerPoint("sp4",length);
		ServerPoint sp5=new ServerPoint("sp5",length);
		Thread t1=new Thread(sp1);
		t1.start();
		t1.setDaemon(true);  //避免线程还认为没有读完而继续读报错，所以和主线程一起down掉
		Thread t2=new Thread(sp2);
		t2.start();
		t2.setDaemon(true);
		Thread t3=new Thread(sp3);
		t3.start();
		t3.setDaemon(true);
		Thread t4=new Thread(sp4);
		t4.start();
		t4.setDaemon(true);
		Thread t5=new Thread(sp5);
		t5.start();
		t5.setDaemon(true);
	    while(length.get(0)<TcpServer.raf.length())
	    {
	    	
	    }
	    pool.shutdown();
	    System.out.println("close");
	    os.close();
	    raf.close();
	    acceptS.close();

	}	
}
class ServerPoint implements Runnable{

	private String name;
	private List<Integer> length;

	/**
	 * @param name
	 * @param length
	 */
	public ServerPoint(String name, List<Integer> length) {
		super();
		this.name = name;
		this.length = length;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

	public List<Integer> getLength() {
		return length;
	}

	public void setLength(List<Integer> length) {
		this.length = length;
	}

	public void run() {
		// TODO Auto-generated method stub
		int m=0;
		try {
				
			while(length.get(0)<TcpServer.raf.length())
			{
				synchronized(length){
					TcpServer.raf.seek(length.get(0));
					m=TcpServer.raf.read(TcpServer.bs);
					if(m>0)
					{
						System.out.println(name+" flag");
						System.out.println(new String(TcpServer.bs));
						TcpServer.os.write(TcpServer.bs,0,m);
						int temp=length.get(0)+m;
						length.set(0, temp);
						System.out.println(name+" read teh file!");
						System.out.println("readed length is : "+length);
						Thread.sleep(100);
					}
				}
			}
			TcpServer.os.close();
			TcpServer. raf.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}